package com.roger.myHome.controller;


import com.roger.myHome.convert.IReqMapper;
import com.roger.myHome.domain.TbMedicalHistory;
import com.roger.myHome.entity.dto.MedicalHistoryAddReq;
import com.roger.myHome.entity.qto.MedicalHistoryQry;
import com.roger.myHome.entity.vo.FinancialDetailVo;
import com.roger.myHome.entity.vo.MedicalHistoryVo;
import com.roger.myHome.page.EUDataGridResult;
import com.roger.myHome.service.MedicalHistoryService;
import com.roger.myHome.util.SystemUtil;
import com.roger.myHome.util.UserHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 病历控制层
 */
@RestController
@RequestMapping("/medicalHistory")
@Slf4j
public class MedicalHistoryController {
    private static List<String> PAGE_LIST = new ArrayList<>();

    static {
        PAGE_LIST = SystemUtil.getMenuUrlList();
    }

    @Resource
    private MedicalHistoryService medicalHistoryService;

    @Resource
    private UserHelper userHelper;


    @PostMapping("/page")
    public EUDataGridResult page(@RequestParam(value="page",required=false) String page,
                                 @RequestParam(value="rows",required=false) String rows, HttpServletRequest req){

        MedicalHistoryQry qry = new MedicalHistoryQry();

        qry.setHospital(req.getParameter("hospital"));

        qry.setPage((Integer.parseInt(page) - 1) * Integer.parseInt(rows));
        qry.setRows(Integer.parseInt(rows));
        qry.setUserId(userHelper.getLoginUser(req).getId());
        return medicalHistoryService.page(qry);
    }

    @PostMapping("/add")
    @ResponseBody
    public String add(HttpServletRequest request, MedicalHistoryAddReq req){
        req.setId(SystemUtil.getId());
        req.setUserId(userHelper.getLoginUser(request).getId());
        TbMedicalHistory entity = IReqMapper.INSTANCE.add2DO(req);
        log.info("entity={}",entity);
        return medicalHistoryService.add(entity);
    }

    @DeleteMapping(value = "/delete/{id}")
    public String delete(@PathVariable Long id){
        log.info("delete:[{}]",id);
        return medicalHistoryService.delete(id);
    }

    @RequestMapping("/navigator")
    public ModelAndView navigator(HttpServletRequest request, HttpServletResponse response){
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort() + path + "/";

        String page = request.getParameter("page");
        //LoginUser loginUser = SystemUtil.getUserFromSession(request);
        Long userId = userHelper.getLoginUser(request).getId();
        if(isLogout(userId, page)){
            try{
                response.reset();
                response.setContentType("text/html;charset=utf-8");
                PrintWriter out = response.getWriter();
                out.println("<html>");
                out.println("<script>");
                out.println("window.open('"+basePath+"','_parent')");
                out.println("</script>");
                out.println("</html>");
            }catch(Exception e){
                System.err.println(e.getMessage());
            }
            return null;
        }else{

            ModelAndView mv = new ModelAndView();
            MedicalHistoryVo vo = medicalHistoryService.getVo(userId,Long.valueOf(request.getParameter("id")));
            mv.addObject("vo", vo);
            mv.addObject("basePath", basePath);
            mv.setViewName(page);
            //mv.setViewName("/home/main");
            return mv;
        }
    }

    private boolean isLogout(Long userId, String page){
        if (PAGE_LIST.contains(page)) {
            return false;
        } else if (userId == null || "logout".equalsIgnoreCase(page)){
            return true;
        } else {
            return true;
        }
    }
}
